<template>
	<view class="">
		<view class="container">
			<view class="header" v-if="!userInfo.id">
				<view class="header-image">
					<image src="/static/images/tab/mine.png" mode="widthFix" style="width: 140rpx;height: 140rpx;">
					</image>
				</view>
				<view class="header-content">
					<view class="name">
						未登录
					</view>
					<view class="require">
						暂无认证
					</view>
					<view class="account">
						账号: ~
					</view>
				</view>
			</view>
			<view class="header" v-else>
				<view class="header-image">
					<image :src="userInfo.head" mode="widthFix" style="width: 160rpx;height: 160rpx;">
					</image>
				</view>
				<view class="header-content">
					<view class="name">
						{{userInfo.name}}
					</view>
					<view class="require">
						{{role == 'role_comp'?'企业认证':role=='role_normal'?'个人认证':'未认证'}}
					</view>
					<view class="account">
						账号: {{userInfo.open_id}}
					</view>
				</view>
				<image src="/static/images/mine/change.png" mode="widthFix" style="width: 40rpx;height: 40rpx;" @click="changeRole" v-if="roleData.length>1"></image>
			</view>
		</view>
		<view class="container">
			<view class="row" v-for="(item,index) of list" :key="index" @click="navigator(item.path,item.type,item.phone)">
				<view class="label">
					{{item.label}}
				</view>
				<view class="arrow">
					<image src="/static/images/mine/arrow.png" mode="widthFix" style="width: 30rpx;height: 30rpx;">
					</image>
				</view>
			</view>
		</view>
		<popLoginComponet ref="login"></popLoginComponet>
	</view>
</template>

<script>
	import {
		userInfo
	} from "@/api/user.js"
	import {
		getRolePage
	} from "@/api/role.js"
	import popLoginComponet from "@/components/popLoginComponent/index.vue"
	import storage from "@/utils/storage.js"
	export default {
		components: {
			popLoginComponet
		},
		data() {
			return {
				list: [{
					label: '我的认证',
					path: '/pages/authentication/index',
					type: 'path'
				}, {
					label: '我的发布',
					path: '/pages/publish/index',
					type: 'tab'
				}, {
					label: '我的收藏',
					path: '/pages/collection_page/index',
					type: 'path'
				}, {
					label: '我的评价',
					path: '/pages/comment_page/index',
					type: 'path'
				}, {
					label: '账户与安全'
				}, {
					label: '联系客服',
					type: 'call',
					phone: '406904512'
				}, {
					label: '隐私政策'
				}],
				userInfo: {},
				role: '',
				roleData: []
			}
		},
		onShow() {
			this.getUserInfo()
		},
		methods: {
			validateLogin() {
				return this.$refs.login.validaLogin()
			},
			changeRole(){
				const data = this.roleData.filter(item => item.role_type != this.role)[0]
				if(data){
					storage.set_user_role(data.role_type)
					this.role = data.role_type
				}
			},
			getUserInfo() {
				if (this.$refs.login.isLogin()) {
					userInfo()
						.then(res => {
							res.open_id = res.open_id.slice(0,10)
							this.userInfo = res
						})
					getRolePage()
					.then(res => {
						this.roleData = (res || []).filter(item => item.audit_status == 1)
						const role_type = storage.get_user_role()
						const has = this.roleData.filter(item => item.role_type == role_type)[0]
						if(!has){
							const data = (res || []).filter(item => item.audit_status == 1)[0]
							if(data){
								storage.set_user_role(data.role_type)
								this.role = data.role_type
							}else{
								storage.set_user_role("")
							}
						}else{
							this.role = role_type
						}
						
					})
				}
			},
			async navigator(path,type,phone){
				await this.validateLogin()
				if(type == 'path'){
					uni.navigateTo({
						url: path
					})
				}else if(type == 'call'){
					uni.makePhoneCall({
						phoneNumber: phone
					})
				}else{
					uni.switchTab({
						url: path
					})
				}
				
			}
		}
	}
</script>

<style>
	page {
		background: #649CF6 linear-gradient(360deg, #fff 0%, #ECF3FF 60%, #F1F6FF 96%, #F1F6FF 100%);
	}
</style>
<style scoped lang="scss">
	.container {
		margin: 20rpx auto;
		padding: 24rpx;
		box-sizing: border-box;
		width: 692rpx;
		background-color: #fff;
		border-radius: 20rpx;

		.header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			.header-image {
				width: 160rpx;
				height: 160rpx;
				background-color: #E9F1FF;
				border-radius: 160rpx;
				overflow: hidden;
				display: flex;
				align-items: center;
				justify-content: space-around;
			}

			.header-content {
				margin-left: 32rpx;
				flex: 1;
				.name {
					font-weight: bold;
					font-size: 38rpx;
					color: #000000;
					line-height: 57rpx;
				}

				.require {
					width: 110rpx;
					text-align: center;
					margin: 10rpx 0;
					padding: 0 10rpx;
					font-weight: 400;
					font-size: 23rpx;
					color: #005DFF;
					line-height: 34rpx;
					background-color: #E9F1FF;
					border-radius: 34rpx;
				}

				.account {
					font-weight: 400;
					font-size: 29rpx;
					color: #979799;
					line-height: 44rpx;
				}
			}

		}

		.row {
			display: flex;
			align-items: center;
			justify-content: space-between;
			line-height: 100rpx;

			.label {
				flex: 1;
				font-weight: 400;
				font-size: 38rpx;
				color: #000000;
			}
		}

		.row+.row {
			border-top: 2rpx solid #F2F2F2;
		}
	}
</style>